//
//  YJinformationViewController.m
//  健康饮食
//
//  Created by qianfeng01 on 15/7/7.
//  Copyright (c) 2015年 qianfeng01. All rights reserved.
//

#import "YJinformationViewController.h"
#import "YJHealthinformationModel.h"
#import "UIImageView+WebCache.h"
@interface YJinformationViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *_tableView;
    UITextView *_text;
    UILabel *_label;
    UIImageView *_imageView;
    NSMutableArray *_dataArray;
    AFHTTPRequestOperationManager *_manager;
    
}

@property (nonatomic,strong)NSMutableArray* dataArray;
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)UITextView *text;
@property (nonatomic,strong)UILabel *label;
@property(nonatomic,strong)YJHealthinformationModel *model;
@property (nonatomic,strong)AFHTTPRequestOperationManager *manager;
@end

@implementation YJinformationViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor greenColor];
    self.tableView=[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.separatorStyle =NO;
     [self.view addSubview:self.tableView];
    
    [self loadData];
   
    
   
}
-(void)addView{
    UIView *View= [[UIView alloc] initWithFrame:self.view.bounds];
    
    _imageView =[[UIImageView alloc] initWithFrame:CGRectMake(10, 30, self.view.frame.size.width-20, self.view.frame.size.height/2)];
  
    [_imageView sd_setImageWithURL:[NSURL URLWithString:_model.img] placeholderImage:[UIImage imageNamed:@"food8"]];
    _label =[[UILabel alloc] initWithFrame:CGRectMake(0, _imageView.frame.size.height-30, self.view.frame.size.width, 30)];
    _label.backgroundColor =[UIColor whiteColor];
    _label.text=_model.title;
    NSCharacterSet *charSet =[NSCharacterSet characterSetWithCharactersInString:@"&nbsp;?。"];
    NSArray *array =[_model.content componentsSeparatedByCharactersInSet:charSet];
    
    NSString *str=[array componentsJoinedByString:@"\n\n"];
    
    _text =[[UITextView alloc] initWithFrame:CGRectMake(0, _imageView.frame.size.height+30, self.view.frame.size.width, _imageView.frame.size.height-40)];
    
    _text.editable =NO;
    _text.text=str;
    [self.dataArray addObject:array];
    
    [View addSubview:_imageView];
    [_imageView addSubview:_label];
    [View addSubview:_text];
    self.tableView.tableHeaderView =View;
   
    
}
-(void)loadData{
   
    AFHTTPRequestOperationManager *manage=[AFHTTPRequestOperationManager manager];
    manage.responseSerializer =[AFHTTPResponseSerializer serializer];
    
    NSString *url =[NSString stringWithFormat:kUrlinformation,(long)self.code];

    [manage GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (responseObject) {
            
            NSDictionary *dicts =[NSJSONSerialization JSONObjectWithData:responseObject options:(NSJSONReadingMutableContainers) error:nil];
            NSDictionary *dict =dicts[@"data"];
            _model =[[YJHealthinformationModel alloc] init];
            _model.title =dict[@"title"];
            _model.content =dict[@"content"];
            _model.img =dict[@"img"];
        }
        [self addView];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    }];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 0;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 
    
    return nil;
}



@end
